#!/bin/bash
set -e
set -x
MYDIR="${0%/*}"
cd "$MYDIR/.."
source "recipes/common.sh"


# RECIPE START bzip2

rm -rf build/bzip2
mkdir -p build
cp -a bzip2 build/
cd build/bzip2
# Thanks God bzip2 does NOT use autocrap
make -j${JOBS} bzip2 bzip2recover install \
	CC=${TARGET}-gcc \
	AR=${TARGET}-ar \
	RANLIB=${TARGET}-ranlib \
	PREFIX=`pwd`/inst${prefix} \
	CFLAGS='-O2 -g -D_FILE_OFFSET_BITS=64 -fpic -fPIC' \

if [ "$target_libdir" != 'lib' ]; then
	mv "inst${prefix}/lib" "inst${prefix}/${target_libdir}"
fi

cd inst
find ${prefix#/} | cpio -pmd "$stage"

cd ../../..
rm -rf build/bzip2

# build a shared library
cp -a bzip2 build/
cd build/bzip2

SOVERSION_MAJOR=`sed -rne 's/^SOVERSION_MAJOR[=]([0-9]+).*$/\1/p' < Makefile-libbz2_so`
if test "x${SOVERSION_MAJOR}" = x; then
	echo "*** Error: bzip2.sh: failed to figure out libbz SONAME" >&2
	exit 1
fi
SONAME="libbz2.so.${SOVERSION_MAJOR}"

make -j${JOBS} -f Makefile-libbz2_so \
	CC=${TARGET}-gcc \
	CFLAGS='-O2 -g -D_FILE_OFFSET_BITS=64 -fpic -fPIC'

# XXX: no make install for the shared library, do it manually
cp -a --target-directory="${stage}${prefix}/${target_libdir}" "${SONAME}" ${SONAME}.*
ln -sf ${SONAME} "${stage}${prefix}/${target_libdir}/libbz2.so"

cd ../..
rm -rf build/bzip2

# RECIPE END bzip2
